/*
当庄模式的发牌器节点，除了发牌功能还有码牌桥的功能
*/
var gameData = require("gameData");
var confige = require("confige")

cc.Class({
    extends: cc.Component,

    properties: {
        cardItem:{
            default:null,
            type:cc.Node
        },
    },

    // use this for initialization
    onLoad: function () {

    },

    onInit:function(gameType){
        this.disAllUser = false;
        if(gameType)
            this.disAllUser = true;

        this.initWall();
        this.cardItemList = {};
        this.cardItemCount = 0;
        this.mainPlayerScale = 1;
        this.otherPlayerScale = 1;
        
        this.playerActiveList = {};
        this.handCardPosList =  {};
        this.handCardList = {};
        this.curPlayerCount = confige.playerMax;
        
        for(var i=0;i<this.curPlayerCount;i++)
        {
            this.handCardList[i] = gameData.gamePlayerNode.playerListNew[i];
        }

        for(var i=0;i<this.curPlayerCount;i++)
        {
            this.handCardPosList[i] = {};
            this.playerActiveList[i] = false;
            for(var j=0;j<5;j++)
            {
                var curCardBackNode = this.handCardList[i].cardsBack[j];
                var newVec1 = this.handCardList[i].node.convertToWorldSpaceAR(cc.v2(curCardBackNode.x,curCardBackNode.y));
                var newVec2 = this.node.convertToNodeSpaceAR(cc.v2(newVec1.x,newVec1.y));
                this.handCardPosList[i][j] = {x:newVec2.x,y:newVec2.y};
            }
        }
       
        this.disRoundCount = 0;
    },

    resetCardList:function(){
        // for(var i=0;i<this.curPlayerCount;i++){
        //     this.playerActiveList[i] = false;
        // }
        for(var i=0;i<this.cardItemCount;i++)
            this.cardItemList[i].destroy();
        this.cardItemCount = 0;
        this.disRoundCount = 0;
    },

    activePlayer:function(index){
        this.playerActiveList[index] = true;
    },

    deActivePlayer:function(index){
        this.playerActiveList[index] = false;
    },

    disCardOneRound:function(){
        console.log("disCardOneRound!!!!!!!!!!!!!!!!!")
        var disCount = 0;
        this.disRoundCount ++;
        var curRoundCount = this.disRoundCount - 1;
        var callBack = function(){
            if(this.disAllUser || this.playerActiveList[disCount] == true)
            {
                var newCardItem = cc.instantiate(this.cardItem);
                this.node.addChild(newCardItem);
                newCardItem.active = true;
                newCardItem.x = -545;
                newCardItem.y = 190;
                this.cardItemList[this.cardItemCount] = newCardItem;
                this.cardItemCount ++;

                var action1 = cc.moveTo(0.2, cc.p(this.handCardPosList[disCount][curRoundCount].x, this.handCardPosList[disCount][curRoundCount].y));
                var scaleNum = 1;
                if(disCount == 0)
                    scaleNum = this.mainPlayerScale;
                else
                    scaleNum = this.otherPlayerScale;
                var action2 = cc.scaleTo(0.2,scaleNum);
                var self = this
                var action3 = (function(disCount,curRoundCount,self) {
                   return cc.callFunc(function () {
                    newCardItem.active = false;
                    self.handCardList[disCount].showCardBackWithIndex(curRoundCount);
                }, self)
                })(disCount,curRoundCount,self)

                newCardItem.runAction(cc.sequence(cc.spawn(action1,action2), cc.delayTime(0.1),action3));
            }            
            disCount ++;
            if(disCount == this.curPlayerCount)
            {
                // this.unschedule(callBack);
                window.protectedUnschedule(this,callBack);
            }
        };

        this.schedule(callBack,0.03);
    },

    disCardWithRoundTime:function(rountTime){
        var callTime = 0;
        var callMax = rountTime;
        var callBack2 = function(){
            this.disCardOneRound();
            callTime ++;
            if(callTime == callMax){
                // this.unschedule(callBack2);
                window.protectedUnschedule(this,callBack2);
            }
        };
        this.schedule(callBack2,0.05);
    },

    initWall:function(){
        this.wallPosListX = [-162,-126,-90,-54,-18,18,54,90,126,162];
        this.wallPosY0 = 125;
        this.wallPosY1 = 131;
        this.wallCardList = {};
        this.dealCardDelay = 0.5;
    },

    createWall:function(){
        for(var i in this.wallCardList)
            this.wallCardList[i].destroy();
        this.wallCardList = {};

        for(var i=0;i<10;i++)
        {
            var newCard0 = cc.instantiate(this.cardItem);
            newCard0.x = this.wallPosListX[i];
            newCard0.y = this.wallPosY0;
            newCard0.active = true;
            this.node.addChild(newCard0);
            var newCard1 = cc.instantiate(this.cardItem);
            newCard1.x = this.wallPosListX[i];
            newCard1.y = this.wallPosY1;
            newCard1.active = true;
            this.node.addChild(newCard1);

            this.wallCardList[i*2] = newCard0;
            this.wallCardList[i*2+1] = newCard1;
        }
        // var self = this;
        // var dealCard = function(){
        //     self.dealCardWithType(0,5);
        // };
        // this.scheduleOnce(dealCard,3);
    },

    dealCardWithType:function(type,beginChair){
        //type:0->left begin;   1->right begin;
        var self = this;
        // if(type == 0)
        // {
            for(var i=0;i<9;i++)
            {
                var curCard0 = this.wallCardList[i*2];
                var curCard1 = this.wallCardList[i*2+1];
                if(type == 1)
                {
                    curCard0 = this.wallCardList[(9-i)*2];
                    curCard1 = this.wallCardList[(9-i)*2+1];
                }
                var curChair = (beginChair+i)%9;
                var endStatus = false;
                if(i == 8)
                    endStatus = true;
                var curDelayTime = this.dealCardDelay*i;
                var dealOnce = function(){
                    window.DKTools.audioEG.playEffect("tips_disCard");
                    var posxOffset = this.handCardPosList[dealOnce.chair][1].x - this.handCardPosList[dealOnce.chair][0].x;
                    var action1 = cc.moveTo(0.3, cc.p(this.handCardPosList[dealOnce.chair][0].x, this.handCardPosList[dealOnce.chair][0].y));
                    var action2 = cc.callFunc(function () {
                        dealOnce.card0.active = false;
                        self.handCardList[dealOnce.chair].showCardBackWithIndex(0);
                    });
                    var action3 = cc.moveTo(0.3, cc.p(this.handCardPosList[dealOnce.chair][0].x, this.handCardPosList[dealOnce.chair][0].y+6));
                    var action4 = cc.callFunc(function () {
                        
                        dealOnce.card1.active = false;
                        self.handCardList[dealOnce.chair].showCardBackWithIndex(1);
                        if(dealOnce.endStatus)
                            if(gameData.gameMainScene.dealCardOverCall){
                                gameData.gameMainScene.dealCardOverCall();
                                self.wallCardList[18].runAction(cc.fadeOut(0.1));
                                self.wallCardList[19].runAction(cc.fadeOut(0.1));
                            }
                    });
                    dealOnce.card0.runAction(cc.sequence(action1, cc.delayTime(0.5),cc.delayTime(0.3),action2));
                    dealOnce.card1.runAction(cc.sequence(action3, cc.delayTime(0.5),cc.moveBy(0.15,cc.p(posxOffset,0)),cc.delayTime(0.1),cc.moveBy(0.15,cc.p(0,-6)),action4));
                };
                dealOnce.card0 = curCard0;
                dealOnce.card1 = curCard1;
                dealOnce.chair = curChair;
                dealOnce.endStatus = endStatus;
                this.scheduleOnce(dealOnce,curDelayTime);
            }
        // }
    },
});
